// 接口域名地址
var api = "http://api.lianc.vip";
// var api = "http://192.168.3.225:8080";
var songapi='http://ws.lianc.vip';
// var songapi="http://192.168.3.225:10033";
var socketapi="ws://ws.lianc.vip/connection.ws" 
// var socketapi="ws://192.168.3.225:10033" 
// post请求
function postRes(url, data, callback) {
  wx.request({
    url: api + url,
    data: data,
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    success: function (res) {
      callback(res.data);
    },
  });
}// post header json请求
function postHeader(url, data, callback) {
  wx.request({
    url: api + url,
    data: data,
    method: "POST",
    header: {
      "Content-Type": "application/json",
    },
    success: function (res) {
      callback(res.data);
    },
  });
}
// get请求，拼接url
function getUrl(url, callback) {
  wx.request({
    url: api + url,
    method: "GET",
    success: function (res) {
      callback(res.data);
    },
  });
}
// get请求，传token
function getToken(url, data, token, callback) {
  wx.request({
    url: api + url,
    data: data,
    method: "GET",
    header: {
      "content-type": "application/x-www-form-urlencoded",
      Authorization: "Bearer " + token,
    },
    success: function (res) {
      callback(res.data);
    },
  });
}
// post请求，传token
function postToken(url, data, token, callback) {
  wx.request({
    url: api + url,
    data: data,
    method: "POST",
    header: {
      "content-type": "application/x-www-form-urlencoded",
      Authorization: "Bearer " + token,
    },
    success: function (res) {
      callback(res.data);
    },
    fail:function (res) {
      callback(res);
    }
  });
}
// 调音功能
function songpostToken(url, data, token, callback) {
  wx.request({
    url: songapi + url,
    data: data,
    method: "POST",
    header: {
      "content-type": "application/x-www-form-urlencoded",
      Authorization: "Bearer " + token,
    },
    success: function (res) {
      callback(res.data);
    },
  });
}
// post请求，url拼接
function postUrl(url, data, callback) {
  wx.request({
    url: api + url + "?keyword=" + data,
    method: "POST",
    success: function (res) {
      callback(res.data);
    },
  });
}
// 歌手列表页获取地区歌手
function singerArea(url, data, callback) {
  wx.request({
    url: api + url,
    data: data,
    method: "POST",
    success: function (res) {
      callback(res.data);
    },
  });
}
// 歌手列表页获取全部的热门歌手
function wholesingerArea(url, data, callback) {
  wx.request({
    url: api + url + "?category=1",
    data: data,
    method: "POST",
    success: function (res) {
      callback(res.data);
    },
  });
}
/**
 * @description: 获取到一个url请求中的参数uid
 * @param        {[type]}   name         [description]  key值
 * @param        {[type]}   url          [description]  url地址
 * @param        {Function} callback     [description]  数据回调函数
 */
function getExtractParam(name, url) {
  var pattern = new RegExp("[\\?&#]" + name + "=([^&#]+)", "gi");
  if (pattern) {
      var ma = (url || location.search).match(pattern);
      var strArr;
      if (ma && ma.length > 0) {
          strArr = (ma[ma.length - 1]).split("=");
          if (strArr && strArr.length > 1) {
              return strArr[1];
          }
          return ""
      } else {
          return ""
      }
  }
};
module.exports = {
  api: api,
  songapi:songapi,
  socketapi:socketapi,
  postRes: postRes,
  postHeader:postHeader,
  getUrl:getUrl,
  getToken: getToken,
  postUrl: postUrl,
  singerArea: singerArea,
  wholesingerArea: wholesingerArea,
  getExtractParam:getExtractParam,
  postToken:postToken,
  songpostToken:songpostToken,
};
